package guhong.play.commandsystem.gui.key.system;

import cn.hutool.core.exceptions.ExceptionUtil;
import guhong.play.commandsystem.CommandManager;
import guhong.play.commandsystem.gui.key.KeyListenerHandler;
import guhong.play.commandsystem.gui.key.type.KeyType;
import guhong.play.commandsystem.gui.ui.UIManger;
import guhong.play.commandsystem.util.print.PrintUtil;
import lombok.Data;

import java.awt.event.KeyEvent;

/**
 * 回车键监听
 *
 * @author : 李双凯
 * @date : 2019-11-20 22:32
 **/
@Data
public class EnterHandler implements KeyListenerHandler {

    /**
     * 是否监听
     *
     * @param e 事件对象
     * @return 监听返回true
     */
    @Override
    public boolean isListener(KeyEvent e) {
        return e.getKeyCode() == KeyEvent.VK_ENTER && '\n' == e.getKeyChar();
    }

    /**
     * 监听类型
     */
    @Override
    public KeyType type() {
        return KeyType.PRINT;
    }


    /**
     * 执行
     */
    @Override
    public void execute() {
        UIManger uiManger = CommandManager.getUiManger();
        String commandStr = uiManger.getCommand();

        try {
            // 执行命令
            CommandManager.execute(commandStr);
        } catch (Exception e) {
            e.printStackTrace();
            PrintUtil.printLnError(ExceptionUtil.getRootCauseMessage(e));
        } finally {
            // 清除命令
            uiManger.clearCommand();
        }

    }


}
